#include "testmatrixsheet.h"
#include "matrixsheet.h"
#include "matrix.h"

#include <QSplitter>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>

TestMatrixSheet::TestMatrixSheet(QWidget *parent) : QWidget(parent)
{
    m_leftSheet = new MatrixSheet(this);
    m_rightSheet = new MatrixSheet(this);

    QSplitter* splitter = new QSplitter(Qt::Horizontal, this);
    splitter->addWidget(m_leftSheet);
    splitter->addWidget(m_rightSheet);

    QPushButton* removeLeftBtn = new QPushButton("Remove Left", this);
    QPushButton* removeRightBtn = new QPushButton("Remove Right", this);
    QPushButton* transposeBtn = new QPushButton("Transpose", this);
    QPushButton* conjugateBtn = new QPushButton("Conjugate", this);
    QPushButton* adjointBtn = new QPushButton("Adjoint", this);

    QHBoxLayout* btnLayout = new QHBoxLayout;
    btnLayout->addWidget(removeLeftBtn);
    btnLayout->addWidget(removeRightBtn);
    btnLayout->addWidget(transposeBtn);
    btnLayout->addWidget(conjugateBtn);
    btnLayout->addWidget(adjointBtn);

    QVBoxLayout* layout = new QVBoxLayout;
    layout->addWidget(splitter);
    layout->addLayout(btnLayout);
    setLayout(layout);

    const int ColumnCount = 10;
    const int RowCount = 10;

    ///
    m_matrix.reset(new FloatMatrix(RowCount, ColumnCount));
    m_matrix->random();

    m_leftSheet->setMatrix(m_matrix);
    m_rightSheet->setMatrix(m_matrix);

    connect(removeLeftBtn, SIGNAL(clicked()), this, SLOT(removeLeft()));
    connect(removeRightBtn, SIGNAL(clicked()), this, SLOT(removeRight()));
    connect(transposeBtn, SIGNAL(clicked()), this, SLOT(transpose()));
    connect(conjugateBtn, SIGNAL(clicked()), this, SLOT(conjugate()));
    connect(adjointBtn, SIGNAL(clicked()), this, SLOT(adjoint()));
}

static auto safeRelease = [](MatrixSheet** obj) -> void {
    if (*obj) {
        delete *obj;
        *obj = nullptr;
    }
};

void TestMatrixSheet::removeLeft()
{
    safeRelease(&m_leftSheet);
}

void TestMatrixSheet::removeRight()
{
    safeRelease(&m_rightSheet);
}

void TestMatrixSheet::transpose()
{
    m_matrix->transpose();
}

void TestMatrixSheet::conjugate()
{
//    m_matrix->conjugate();
}

void TestMatrixSheet::adjoint()
{
    m_matrix->adjoint();
}


